package com.lnkj.spcj.util;

import android.content.Context;
import android.graphics.Bitmap;

import androidx.core.graphics.drawable.RoundedBitmapDrawable;
import androidx.core.graphics.drawable.RoundedBitmapDrawableFactory;

import android.text.TextUtils;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.BitmapImageViewTarget;
import com.lnkj.spcj.util.utilcode.EncodeUtils;
import com.lnkj.spcj.net.UrlUtils;
import com.lnkj.spcj.R;

import jp.wasabeef.glide.transformations.BlurTransformation;

/**
 * Copyright (c) 山东六牛网络科技有限公司 https://liuniukeji.com
 *
 * @Description
 * @Author 与天同行的观测者
 * @Copyright Copyright (c) 山东六牛网络科技有限公司 保留所有版权(https://www.liuniukeji.com)
 * @Date 2017/12/11 0011 14:18
 */

public class ImageLoader {

    public static void loadHead(final Context context, ImageView view, String url) {
        try {
            url = EncodeUtils.htmlDecode(url).toString();
            if (!TextUtils.isEmpty(url) && !url.contains("http"))
                url = new UrlUtils().getAPIHTTP() + url;
            RequestOptions options = new RequestOptions();
            options.centerCrop().placeholder(R.drawable.default_avatar).error(R.drawable.default_avatar);
            Glide.with(context).asBitmap()
                    .load(url)
                    .apply(options)
                    .into(new BitmapImageViewTarget(view) {
                        @Override
                        protected void setResource(Bitmap resource) {
                            RoundedBitmapDrawable circularBitmapDrawable = RoundedBitmapDrawableFactory.create(context.getResources(), resource);
                            circularBitmapDrawable.setCircular(true);
                            view.setImageDrawable(circularBitmapDrawable);
                        }
                    });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void loadHead2(final Context context, ImageView view, String url) {
        try {
            url = EncodeUtils.htmlDecode(url).toString();
            if (!TextUtils.isEmpty(url) && !url.contains("http"))
                url = new UrlUtils().getAPIHTTP() + url;
            RequestOptions options = new RequestOptions();
            options.centerCrop().placeholder(R.drawable.default_avatar).error(R.drawable.default_avatar);
            Glide.with(context)
                    .load(url)
                    .apply(options)
                    .into(view);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void loadImage(Context context, ImageView view, String url) {
        try {
            url = EncodeUtils.htmlDecode(url).toString();
            if (!TextUtils.isEmpty(url) && !url.contains("http"))
                url = new UrlUtils().getAPIHTTP() + url;
            RequestOptions options = new RequestOptions();
            options.centerCrop().placeholder(R.drawable.default_image).error(R.drawable.default_image).diskCacheStrategy(DiskCacheStrategy.ALL);
            Glide.with(context)
                    .load(url)
                    .apply(options)
                    .into(view);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void loadImageNoDefault(Context context, ImageView view, String url) {
        try {
            url = EncodeUtils.htmlDecode(url).toString();
            if (!TextUtils.isEmpty(url) && !url.contains("http"))
                url = new UrlUtils().getAPIHTTP() + url;
            RequestOptions options = new RequestOptions();
            options.diskCacheStrategy(DiskCacheStrategy.ALL);
            Glide.with(context)
                    .load(url)
                    .apply(options)
                    .into(view);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void loadHeadLocal(final Context context, ImageView view, String url) {
        try {
            RequestOptions options = new RequestOptions();
            options.centerCrop().placeholder(R.drawable.default_avatar).error(R.drawable.default_avatar).skipMemoryCache(true).diskCacheStrategy(DiskCacheStrategy.NONE);
            Glide.with(context)
                    .asBitmap()
                    .load(url)
                    .apply(options)
                    .into(new BitmapImageViewTarget(view) {
                        @Override
                        protected void setResource(Bitmap resource) {
                            RoundedBitmapDrawable circularBitmapDrawable =
                                    RoundedBitmapDrawableFactory.create(context.getResources(), resource);
                            circularBitmapDrawable.setCircular(true);
                            view.setImageDrawable(circularBitmapDrawable);
                        }
                    });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void loadImageLocal(final Context context, ImageView view, String url) {
        try {
            RequestOptions options = new RequestOptions();
            options.centerCrop().placeholder(R.drawable.default_image).error(R.drawable.default_image).skipMemoryCache(true).diskCacheStrategy(DiskCacheStrategy.NONE);
            Glide.with(context)
                    .asBitmap()
                    .load(url)
                    .apply(options)
                    .into(view);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void loadHeadDefault(final Context context, ImageView view, String url, int defaultRes) {
        try {
            if (!TextUtils.isEmpty(url) && !url.contains("http"))
                url = new UrlUtils().getAPIHTTP() + url;
            RequestOptions options = new RequestOptions();
            options.centerCrop().placeholder(defaultRes).error(defaultRes);
            Glide.with(context).asBitmap()
                    .load(url)
                    .apply(options)
                    .into(new BitmapImageViewTarget(view) {
                        @Override
                        protected void setResource(Bitmap resource) {
                            RoundedBitmapDrawable circularBitmapDrawable = RoundedBitmapDrawableFactory.create(context.getResources(), resource);
                            circularBitmapDrawable.setCircular(true);
                            view.setImageDrawable(circularBitmapDrawable);
                        }
                    });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void loadImageDefault(Context context, ImageView view, String url, int defaultRes) {
        try {
            if (!TextUtils.isEmpty(url) && !url.contains("http"))
                url = new UrlUtils().getAPIHTTP() + url;
            RequestOptions options = new RequestOptions();
            options.centerCrop().placeholder(defaultRes).error(defaultRes);
            Glide.with(context)
                    .load(url)
                    .apply(options)
                    .into(view);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void loadImageCover(Context context, ImageView view, String url) {
        try {
            if (!TextUtils.isEmpty(url) && !url.contains("http"))
                url = new UrlUtils().getAPIHTTP() + url;
            RequestOptions options = new RequestOptions();
            Glide.with(context)
                    .asBitmap()
                    .load(url)
                    .apply(options.centerCrop())
                    .into(view);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void loadImageGaoSi(Context context, ImageView view, String url) {
        try {
            if (!TextUtils.isEmpty(url) && !url.contains("http"))
                url = new UrlUtils().getAPIHTTP() + url;
            Glide.with(context)
                    .asBitmap()
                    .load(url)
                    .apply(RequestOptions.bitmapTransform(new BlurTransformation(20)))
                    .into(view);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void loadImageLiu(Context context, ImageView view, String url, int width, int height, int defaultWidth) {
        try {
            ViewGroup.LayoutParams layoutParams = (ViewGroup.LayoutParams) view.getLayoutParams();//获取你要填充图片的布局的layoutParam
            int downloadWidth = 0;
            int downloadHeight = 0;
            if (width != 0 && height != 0) {//如果宽度和高度都能获取到,根据比例计算ImageView的大小
                layoutParams.height = (int) (((float) height) / width * defaultWidth);
                layoutParams.width = defaultWidth;//这个是布局的宽度
                view.setLayoutParams(layoutParams);//容器的宽高设置好了
            } else {//如果宽度和高度获取不到，就让它们等于默认值，此处是两列的RecyclerView,所以defaultWidth等于屏幕一般的宽度减去RecylcerView的间隔和margin(eg:ScreenUtils.getScreenWidth() / 2 - SizeUtils.dp2px(10f))
                layoutParams.height = defaultWidth;
                layoutParams.width = defaultWidth;//这个是布局的宽度
                view.setLayoutParams(layoutParams);//容器的宽高设置好了
            }
            downloadWidth = layoutParams.width;
            downloadHeight = layoutParams.height;
            url = EncodeUtils.htmlDecode(url).toString();
            if (!TextUtils.isEmpty(url) && !url.contains("http"))
                url = new UrlUtils().getAPIHTTP() + url;
            RequestOptions options = new RequestOptions();
            options.centerCrop().placeholder(R.drawable.default_image).error(R.drawable.default_image).diskCacheStrategy(DiskCacheStrategy.ALL);
            Glide.with(context)
                    .asBitmap()
                    .load(url)
                    .apply(options)
                    .into(view);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static String getUrl(String url) {
        url = EncodeUtils.htmlDecode(url).toString();
        if (!TextUtils.isEmpty(url) && !url.contains("http"))
            url = new UrlUtils().getAPIHTTP() + url;
        return url;
    }
}
